package com.api.controller;

import cn.hutool.core.util.StrUtil;
import com.api.entity.SysConfig;
import com.api.log.Log;
import com.api.mapper.SysConfigMapper;
import com.api.utils.ParamsUtils;
import com.api.utils.Result;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.api.entity.table.SysConfigTableDef.SYS_CONFIG;
import static com.api.entity.table.SysDeptTableDef.SYS_DEPT;
import static com.api.entity.table.UserTableDef.USER;

/**
 * @Author: yangsf
 * @CreateDate: 2024/10/29
 */

@Slf4j
@RestController
@RequestMapping("/sysConfig")
public class SysConfigController extends BaseController {


    @Autowired
    SysConfigMapper configMapper;

    @Log("获取系统配置")
    @RequestMapping("/getList")
    public Result getList(@RequestParam Map<String,Object> params){
        QueryWrapper qw = QueryWrapper.create();
        qw.select(SYS_CONFIG.ALL_COLUMNS,USER.USER_NAME)
                .leftJoin(USER).on(USER.ID.eq(SYS_CONFIG.CREATE_ID))
            .where(SYS_CONFIG.STATUS.eq(1));
        if(params.containsKey("nameKeyword") && null != params.get("nameKeyword") && !StringUtils.isEmpty(params.get("nameKeyword"))){
            String nameKeyword = params.get("nameKeyword").toString();
            qw.and(SYS_CONFIG.DATA1.like(nameKeyword));
        }
        if(params.containsKey("configKind") && null != params.get("configKind") && !StringUtils.isEmpty(params.get("configKind"))){
            String configKind = params.get("configKind").toString();
            qw.and(SYS_CONFIG.CONFIG_KIND.like(configKind));
        }
        if(params.containsKey("configType") && null != params.get("configType") && !StringUtils.isEmpty(params.get("configType"))){
            String configType = params.get("configType").toString();
            qw.and(SYS_CONFIG.CONFIG_TYPE.like(configType));
        }
        qw.orderBy(SYS_CONFIG.ID,Boolean.FALSE);
        Page page = ParamsUtils.toPage(params);
        Page<SysConfig> pages =configMapper.paginate(page,qw);
        return Result.ok(pages);
    }


    @Log("保存系统配置")
    @RequestMapping("/save")
    public Result save(@RequestBody SysConfig config){
        Long userId = getUserId();
        if(null == config.getId()){
            config.setCreateId(userId);
        }
        config.setUpdateId(userId);
        //设置默认值
        config.setStatus(1);
        int i = configMapper.insertOrUpdate(config);
        if(i>0){
            if(StrUtil.isBlank(config.getConfigType())){
                if("ip_white_list".equals(config.getConfigType())){
                    redisService.deleteObject("ip_white_list");
                }
                else if("restrict_login_time".equals(config.getConfigType())){
                    int restrict_login_time_start = Integer.parseInt(config.getData1());
                    int restrict_login_time_end = Integer.parseInt(config.getData2());
                    redisService.setCacheObject("restrict_login_time_start", restrict_login_time_start,8L, TimeUnit.HOURS);
                    redisService.setCacheObject("restrict_login_time_end", restrict_login_time_end,8L, TimeUnit.HOURS);
                }
            }
            return  Result.ok();
        }
        return Result.err();
    }


    @Log("刪除系统配置")
    @RequestMapping("/delete")
    public Result delete(Long id){
        if(null == id){
            return Result.err("未获取到菜单ID");
        }
        boolean flag = SysConfig.create()
                .setId(id)
                .setStatus(0)
                .setUpdateId(getUserId())
                .updateById();
        if(flag){
            return Result.ok();
        }
        return Result.err();
    }



}
